/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.project.web.projectbasicinfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.common.utils.Constant;
import com.idenger.modules.project.entity.project.Project;
import com.idenger.modules.project.entity.projecttype.ProjectType;
import com.idenger.modules.project.service.project.ProjectService;
import com.idenger.modules.project.service.projecttype.ProjectTypeService;
import com.idenger.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.config.Global;
import com.idenger.common.persistence.Page;
import com.idenger.common.web.BaseController;
import com.idenger.common.utils.StringUtils;
import com.idenger.modules.project.entity.projectbasicinfo.ProjectBasicinfo;
import com.idenger.modules.project.service.projectbasicinfo.ProjectBasicinfoService;

import java.util.List;

/**
 * 项目基本信息Controller
 * @author chenlin
 * @version 2018-04-11
 */
@Controller
@RequestMapping(value = "${adminPath}/project/projectbasicinfo/projectBasicinfo")
public class ProjectBasicinfoController extends BaseController {

	@Autowired
	private ProjectBasicinfoService projectBasicinfoService;

	@Autowired
	private ProjectTypeService projectTypeService;

	@Autowired
	private ProjectService projectService;


	@ModelAttribute
	public ProjectBasicinfo get(@RequestParam(required=false) String id) {
		ProjectBasicinfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = projectBasicinfoService.get(id);
		}
		if (entity == null){
			entity = new ProjectBasicinfo();
		}
		return entity;
	}

	@RequiresPermissions("project:projectbasicinfo:projectBasicinfo:view")
	@RequestMapping(value = {"list", ""})
	public String findProjectList(ProjectBasicinfo projectBasicinfo,HttpServletRequest request, HttpServletResponse response, Model model) {

		//是否拥有所有的数据权限
		boolean bln = UserUtils.getAllDataRole();
		if(bln){
			projectBasicinfo.setCreatorId(UserUtils.getUser().getId());
		}
		String  projectName = request.getParameter("projectName");
		if(StringUtils.isNotBlank(projectName)){
			projectBasicinfo.setProjectName(projectName);
		}
		Page<ProjectBasicinfo> page = projectBasicinfoService.findProjectList(new Page<ProjectBasicinfo>(request, response), projectBasicinfo);
		model.addAttribute("page", page);

		return "modules/project/projectbasicinfo/projectBasicinfoList";
	}

	@RequiresPermissions("project:projectbasicinfo:projectBasicinfo:view")
	@RequestMapping(value = "form")
	public String form(HttpServletRequest request,ProjectBasicinfo projectBasicinfo, Model model) {
		//查询项目分类
		List<ProjectType> projectTypeList = projectTypeService.findList(new ProjectType());
		Project project = projectService.get(projectBasicinfo.getProjectId());
		model.addAttribute("project",project);
		model.addAttribute("projectTypeList",projectTypeList);
		model.addAttribute("projectBasicinfo", projectBasicinfo);
		return "modules/project/projectbasicinfo/projectBasicinfoForm";
	}

	@RequiresPermissions("project:projectbasicinfo:projectBasicinfo:edit")
	@RequestMapping(value = "save")
	public String save(HttpServletRequest request,ProjectBasicinfo projectBasicinfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, projectBasicinfo)){
			return form(request,projectBasicinfo, model);
		}
		//保存项目,如果有项目ID 则是修改 没有则是添加
		if(!(StringUtils.isNotBlank(projectBasicinfo.getProjectId()))){
			Project project = new Project();
			project.setProjectName("项目名字");
			project.setCreatorId(UserUtils.getUser().getId());//项目创建者
			project.setProjectTypeId("74deebb554da4906b0186174ad9560b6");//项目的分类
			project.setApprovalStatus(Constant.APPROVAL_STATUS_UNAPPROVED);//未审批状态
			project.setDeleteFlag(Constant.NORMAL_STATUS);
			projectService.save(project);
			projectBasicinfo.setProjectId(project.getId());
		}
		projectBasicinfo.setDeleteFlag(Constant.NORMAL_STATUS);
		projectBasicinfoService.save(projectBasicinfo);
		addMessage(redirectAttributes, "保存项目基本信息成功");
		return "redirect:"+Global.getAdminPath()+"/project/projectbasicinfo/projectBasicinfo/findProjectList/?repage";
	}
	
	@RequiresPermissions("project:projectbasicinfo:projectBasicinfo:edit")
	@RequestMapping(value = "delete")
	public String delete(ProjectBasicinfo projectBasicinfo, RedirectAttributes redirectAttributes) {
		projectBasicinfoService.delete(projectBasicinfo);
		projectService.delete(projectService.get(projectBasicinfo.getProjectId()));
		addMessage(redirectAttributes, "删除项目基本信息成功");
		return "redirect:"+Global.getAdminPath()+"/project/projectbasicinfo/projectBasicinfo/?repage";
	}

}